/**
 *
 */
package de.cbf.cam.dsa.entity.edit.talent.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.cbf.cam.dsa.entity.edit.talent.act.ActivatorEditEntityTalent;
import de.cbf.cam.dsa.entity.edit.talent.persistence.ControllerPortXMLTalent;
import de.cbf.cam.dsa.entity.edit.talent.views.EntityTalentsView;
import de.cbf.cam.dsa.facet.talent.FacetTalent;
import de.cbf.cam.dsa.facet.talent.edit.ControllerEditorFacetTalent;
import de.cbf.util.entity.edit.controller.customizing.IFacetControllerCustomizer;
import de.cbf.util.entity.edit.port.AbstractEditEntityMainControllerPort;
import de.cbf.util.entity.edit.service.IServiceFacetEditController;
import de.cbf.util.entity.facet.edit.controller.AbstractControllerEditFacet;
import de.cbf.util.entity.facet.interfaces.IFacetOfEntity;
import de.cbf.util.entity.facet.persistence.resolver.IServiceResolverEntity;
import de.cbf.util.entity.facet.persistence.xml.controller.AbstractControllerPortXMLFacet;

/**
 * @author Nebelritter
 * 
 */
public class EditTalentsMainController extends AbstractEditEntityMainControllerPort {
	private static final Log logger = LogFactory.getLog(EditTalentsMainController.class);

	protected static EditTalentsMainController instance;

	protected ControllerPortXMLTalent imExPortController;
	
	/**
	 *
	 */
	private EditTalentsMainController() {
		logger.trace("creating: "+EditTalentsMainController.class.getSimpleName());
	}
	
	/**
	 * @return the instance
	 */
	public static EditTalentsMainController getInstance() {
		if(instance == null){
			instance = new EditTalentsMainController();
		}
		return instance;
	}

	@Override
	protected Class<? extends IFacetOfEntity> getActiveBaseFacet() {		
		return FacetTalent.class;
	}

	@Override
	protected String getViewID() {		
		return EntityTalentsView.ID;
	}

	@Override
	protected String getLogViewSecondaryID() {		
		return  ActivatorEditEntityTalent.LOG_VIEW_TALENT_EDIT_SEC_ID;
	}

	@Override
	protected List<Class<? extends IFacetOfEntity>> getFacetClasses() {		
		List<Class<? extends IFacetOfEntity>> resultList = new ArrayList<Class<? extends IFacetOfEntity>>();
		resultList.add(FacetTalent.class);
		return resultList ;
	}

	@Override
	protected IServiceFacetEditController getServiceFacetEditController() {
		return ActivatorEditEntityTalent.getDefault().getServiceFacetEditController();
	}

	@Override
	protected IServiceResolverEntity getResolverForEntity() {
		IServiceResolverEntity service = ActivatorEditEntityTalent.getDefault().getServiceResolverEntity();
		if(service != null){			
			return service;
		}else{
			logger.warn("No resolver service found for EntityTalent, will not be able to persist/resolve");
			return null;
		}		
	}
	
	@Override
	protected AbstractControllerPortXMLFacet getImportController() {
		if(imExPortController == null){
			imExPortController = new ControllerPortXMLTalent();
		}
		return imExPortController;
	}

	@Override
	protected AbstractControllerPortXMLFacet getExportController() {
		if(imExPortController == null){
			imExPortController = new ControllerPortXMLTalent();
		}
		return imExPortController;
	}
	
	protected Map<Class<?>, IFacetControllerCustomizer> generateControllerCustomizerConfig() {
		//generate customizer for facetTalentController to provide entities for choice of compensation talents
		Map<Class<?>, IFacetControllerCustomizer> result = new HashMap<Class<?>, IFacetControllerCustomizer>();
		
		final IServiceResolverEntity serviceResolverTalents = ActivatorEditEntityTalent.getDefault().getServiceResolverEntity();
		
		IFacetControllerCustomizer customizerTalents = new IFacetControllerCustomizer() {
			//this will customize talent edit controller to set all talent entities from resolver, to make choice of 
			//compensation talent possible
			@Override			
			public boolean customizeFacetController(AbstractControllerEditFacet<?> controller) {
				if (controller instanceof ControllerEditorFacetTalent) {
					ControllerEditorFacetTalent editorFacetTalent = (ControllerEditorFacetTalent) controller;
					editorFacetTalent.setTalentResolver(serviceResolverTalents);
					return true;
				}
				return false;
			}
		};
		result.put(FacetTalent.class, customizerTalents);
		return result;
	}
}
